package rmi;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;

import Common.GoggleDB;
import Metadata.HeapfileInterface;
import Metadata.attrSchema;
import Transaction.Transaction;


public class RemoteDbMetaImpl extends UnicastRemoteObject implements RemoteDbMeta {
	String dbName ; 
	
	public RemoteDbMetaImpl ( String dbName ) throws RemoteException {	
		this.dbName = dbName ; 
	}
	
	public RemoteResultSet getTables() throws RemoteException {
		try {
			ArrayList < String > list = new ArrayList < String > () ;  
			for ( String str : GoggleDB.storageInterface.get_tables_by_db(dbName + "." ) ) 
				if ( str.indexOf( "!@#$%" ) == -1 ) list.add( str );
			
			return new RemoteResultSetDB ( list ) ;
		}catch ( Exception e ) {
			throw new RemoteException ( e.toString() ) ; 
		}
	}

	public RemoteResultSet getColumn( String tableName ) throws RemoteException {
		ArrayList <String > result = new ArrayList < String > () ;  
		try {
			Transaction tx = new Transaction () ; 
			attrSchema [] res = HeapfileInterface.getTableInfo( dbName + "." + tableName, tx) ;
			for ( attrSchema as : res ) 
				if ( as.pk != 0  ) result.add( as.attrName ) ; 
			return new RemoteResultSetDB ( result ) ; 
		}catch ( Exception e ) {
//			e.printStackTrace() ;
			throw new RemoteException ( e.toString() ) ; 
		}
	}

	@Override
	public RemoteResultSet getIndex(String tableName) throws RemoteException {
		ArrayList <String > result = new ArrayList < String > () ;  
		try {
			Transaction tx = new Transaction () ; 
			attrSchema [] res = HeapfileInterface.getTableInfo( dbName + "." + tableName, tx) ;
			for ( attrSchema as : res ) 
				if ( as.fk ) result.add( as.attrName ) ; 
			return new RemoteResultSetDB ( result ) ; 
		}catch ( Exception e ) {
		//	e.printStackTrace() ; 
			throw new RemoteException ( e.toString() ) ; 
		}
	}

}
